package com.msun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.msun.entity.PageResult;
import com.msun.entity.QueryPageBean;
import com.msun.entity.Result;
import com.msun.pojo.Menu;
import com.msun.pojo.Permission;
import com.msun.pojo.Role;
import com.msun.service.RoleService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查项管理
 */

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference//查找服务
    private RoleService roleService;


    //编辑
    @RequestMapping("/delete")
    public Result delete(Integer id){
        System.out.println(id);
        try{
            roleService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "删除失败");
        }
        return  new Result(true,"删除成功");
    }


    //编辑
    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role){
        System.out.println(permissionIds);
        System.out.println(menuIds);
        try{
            roleService.edit(role,permissionIds,menuIds);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "编辑失败");
        }
        return  new Result(true,"编辑");
    }


   ///新增
    @RequestMapping("/add")
    public Result add(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role){
        System.out.println(permissionIds);
        System.out.println(menuIds);
        try{
            roleService.add(role,permissionIds,menuIds);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "新增失败");
        }
        return  new Result(true,"新增成功");
    }

    //检查项分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;
    }

 @RequestMapping("/findAllPermissions")
 public Result findAllPermissions(){
     try{
         List<Permission> list = roleService.findAllPermissions();
         return  new Result(true, "查询成功",list);
     }catch (Exception e){
         e.printStackTrace();
         //服务调用失败
         return new Result(false,"查询失败");
     }
 }
    @RequestMapping("/findAllMenus")
    public Result findAllMenus(){
        try{
            List<Menu> list = roleService.findAllMenus();
            return  new Result(true, "查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false,"查询失败");
        }
    }
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        try {
            List<Integer> list= roleService.findMenuIdsByRoleId(id);
            return new Result(true, "查询成功成功",list);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败失败");
        }
    }

    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> list= roleService.findPermissionIdsByRoleId(id);
            return new Result(true, "查询成功成功",list);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败失败");
        }
    }
    @RequestMapping("/findAll/{userId}")
    public Result findAll(@PathVariable("userId") Integer userId){
        List<Role> allRoles = roleService.findAllCheckGroupByUserId(userId);
        return new Result(true,"",allRoles);
    }

}
